﻿
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JoyfulHomeJourney.BackEnd.Api.Admin
{
    public static class Inject_Helper
    {
        public static void AddScopedRange(this IServiceCollection services,List<Type> interfaces,List<Type> instances)
        {
            instances.ForEach(d =>
            {
                var parentInterface = interfaces.Where(e => e.IsAssignableFrom(d)).FirstOrDefault();
                if (parentInterface != null)
                {
                    services.AddScoped(parentInterface, d);
                }
                else
                {
                    //如果只有inject特性将注入它本身，也就是创建本身的实例,也只能用本身的类型接收(父类也不能接收)
                    services.AddScoped(d);
                }
            });
        }
    }
}
